package cn.wangkai.peanut.system.dao.sequence;


import java.sql.SQLException;
import java.util.Calendar;

import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.wangkai.peanut.db.DbFactory;
import cn.wangkai.peanut.db.MyDb;
import cn.wangkai.peanut.db.WKDbException;
import cn.wangkai.peanut.db.iface.IDbFactory;
import cn.wangkai.peanut.db.iface.IMyDb;
import cn.wangkai.peanut.db.util.ManagerException;
import cn.wangkai.peanut.system.db.PeanutDb;
import cn.wangkai.peanut.system.model.Nodtl;



public class SequenceManagerImp implements SequenceManager {
	
	private static final Log log = LogFactory.getLog(SequenceManagerImp.class);
	private IDbFactory dbFactory = new DbFactory();
	private PeanutDb db;
	
	public SequenceManagerImp() {
		db = PeanutDb.Open();
	}
	public String getNextSequence(String noid,int noyear,int nomonth,int noday)  throws SQLException, WKDbException, ManagerException, WKDbException {
		IMyDb myDb = MyDb.Open(db);
		try{
			myDb.BeginTransaction();
			Nodtl nodtl = dbFactory.getOne(myDb, "SELECT * FROM "+Nodtl.TABLENAME+" WHERE noid=? AND noyear=? AND nomonth=? AND noday=?", Nodtl.class, noid,noyear,nomonth,noday);
			if(nodtl==null){
				nodtl = new Nodtl();
				nodtl.setNoid(noid);
				nodtl.setNoyear(noyear);
				nodtl.setNomonth(nomonth);
				nodtl.setNoday(noday);
				nodtl.setNextid(1);
				dbFactory.addselect(myDb, nodtl);
				nodtl = dbFactory.getOne(myDb, "SELECT * FROM "+Nodtl.TABLENAME+" WHERE noid=? AND noyear=? AND nomonth=? AND noday=?", Nodtl.class, noid,noyear,nomonth,noday);
				if(nodtl == null) throw new Exception("数据tb_sys_nodtl新增失败！");
			}
			dbFactory.execute(myDb, "UPDATE "+Nodtl.TABLENAME+" SET nextid=nextid+1 WHERE noid=? AND noyear=? AND nomonth=? AND noday=?", noid,noyear,nomonth,noday);
			myDb.Commit();
			return (nodtl.getPrefix()==null?"":nodtl.getPrefix())+nodtl.getNextid()+(nodtl.getPostfix()==null?"":nodtl.getPostfix());
		}catch (Exception e) {
			myDb.Rollback();
			log.error(e);
			throw new WKDbException(e);
		}finally{
			myDb.Close();
		}
	}
	
	public String getYearNextSequence(String noid)  throws SQLException, WKDbException, ManagerException, WKDbException {
		Calendar cal = Calendar.getInstance();
		return getNextSequence(noid, cal.get(Calendar.YEAR),0,0);
	}
	
	public String getmonthNextSequence(String noid)  throws SQLException, WKDbException, ManagerException, WKDbException {
		Calendar cal = Calendar.getInstance();
		return getNextSequence(noid, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH)+1,0);
	}
	
	public String getToDayNextSequence(String noid)  throws SQLException, WKDbException, ManagerException, WKDbException {
		Calendar cal = Calendar.getInstance();
		return getNextSequence(noid, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH)+1, cal.get(Calendar.DATE));
	}
	
	public String getNextSequence(String noid)  throws SQLException, WKDbException, ManagerException, WKDbException {
		return getNextSequence(noid, 0,0,0);
	}

	public long getNextLongSequence(String noid,int noyear,int nomonth,int noday)  throws SQLException, WKDbException, ManagerException, WKDbException {
		String sequence = getNextSequence(noid, noyear, nomonth, noday);
		return NumberUtils.toLong(sequence);
	}
	public long getYearNextLongSequence(String noid)  throws SQLException, WKDbException, ManagerException, WKDbException {
		String sequence = getYearNextSequence(noid);
		return NumberUtils.toLong(sequence);
	}
	public long getmonthNextLongSequence(String noid)  throws SQLException, WKDbException, ManagerException, WKDbException {
		String sequence = getmonthNextSequence(noid);
		return NumberUtils.toLong(sequence);
	}
	public long getToDayNextLongSequence(String noid)  throws SQLException, WKDbException, ManagerException, WKDbException {
		String sequence = getToDayNextSequence(noid);
		return NumberUtils.toLong(sequence);
	}
	public long getNextLongSequence(String noid)  throws SQLException, WKDbException, ManagerException, WKDbException {
		String sequence = getNextSequence(noid);
		return NumberUtils.toLong(sequence);
	}
	
	public int getNextIntSequence(String noid,int noyear,int nomonth,int noday)  throws SQLException, WKDbException, ManagerException, WKDbException {
		String sequence = getNextSequence(noid, noyear, nomonth, noday);
		return NumberUtils.toInt(sequence);
	}
	public int getYearNextIntSequence(String noid)  throws SQLException, WKDbException, ManagerException, WKDbException {
		String sequence = getYearNextSequence(noid);
		return NumberUtils.toInt(sequence);
	}
	public int getmonthNextIntSequence(String noid)  throws SQLException, WKDbException, ManagerException, WKDbException {
		String sequence = getmonthNextSequence(noid);
		return NumberUtils.toInt(sequence);
	}
	public int getToDayNextIntSequence(String noid)  throws SQLException, WKDbException, ManagerException, WKDbException {
		String sequence = getToDayNextSequence(noid);
		return NumberUtils.toInt(sequence);
	}
	public int getNextIntSequence(String noid)  throws SQLException, WKDbException, ManagerException, WKDbException {
		String sequence = getNextSequence(noid);
		return NumberUtils.toInt(sequence);
	}
	
	public static void main(String[] args) {
		SequenceManager sequenceManager = new SequenceManagerImp();
		try {
			System.out.println(sequenceManager.getmonthNextIntSequence("a_b_c"));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
